package com.soFun.controller;

import com.soFun.service.AiChainService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 对话控制器，提供会话相关API端点
 */
@RestController
@RequestMapping("/conversation")
@RequiredArgsConstructor
public class ConversationController {

    private final AiChainService aiChainService;

    /**
     * 创建新会话
     * @return 包含会话ID的响应
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createConversation() {
        String conversationId = aiChainService.createConversation();
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("conversationId", conversationId);
        response.put("timestamp", System.currentTimeMillis());
        
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }
    
    /**
     * 发送消息到会话
     * @param conversationId 会话ID
     * @param requestBody 包含消息的请求体
     * @return 包含AI回复的响应
     */
    @PostMapping("/{conversationId}/message")
    public ResponseEntity<Map<String, Object>> sendMessage(
            @PathVariable String conversationId,
            @RequestBody Map<String, String> requestBody) {
        
        String message = requestBody.get("message");
        if (message == null || message.trim().isEmpty()) {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "error");
            error.put("message", "消息内容不能为空");
            return ResponseEntity.badRequest().body(error);
        }
        
        try {
            String response = aiChainService.sendMessage(conversationId, message);
            
            Map<String, Object> result = new HashMap<>();
            result.put("status", "success");
            result.put("message", response);
            result.put("conversationId", conversationId);
            result.put("timestamp", System.currentTimeMillis());
            
            return ResponseEntity.ok(result);
        } catch (IllegalArgumentException e) {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "error");
            error.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
        }
    }
    
    /**
     * 获取会话信息
     * @param conversationId 会话ID
     * @return 包含会话信息的响应
     */
    @GetMapping("/{conversationId}")
    public ResponseEntity<Map<String, Object>> getConversationInfo(@PathVariable String conversationId) {
        try {
            Map<String, Object> info = aiChainService.getConversationInfo(conversationId);
            info.put("status", "success");
            return ResponseEntity.ok(info);
        } catch (IllegalArgumentException e) {
            Map<String, Object> error = new HashMap<>();
            error.put("status", "error");
            error.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(error);
        }
    }
    
    /**
     * 删除会话
     * @param conversationId 会话ID
     * @return 删除操作结果
     */
    @DeleteMapping("/{conversationId}")
    public ResponseEntity<Map<String, Object>> deleteConversation(@PathVariable String conversationId) {
        aiChainService.deleteConversation(conversationId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("status", "success");
        response.put("message", "会话已删除");
        response.put("conversationId", conversationId);
        
        return ResponseEntity.ok(response);
    }
} 